package com.qf.controller;

import com.qf.entity.Product;
import com.qf.entity.Shop;
import com.qf.entity.custom.ShopCustom;
import com.qf.service.ShopCustomService;
import com.qf.service.UserBrowseService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/23 21:50
 */
@RestController
@RequestMapping("/shopCustomController")
public class ShopCustomController {
    @Autowired
    private ShopCustomService shopCustomService;
    @Autowired
    private UserBrowseService userBrowseService;

    /**
     *
     * 首页(面包甜点and商品信息)：商品信息传null 搜索全部店铺以及商品信息
     *                            面包甜点中 全部条件 传null 搜索全部店铺以及商品信息
     *                            其它条件传字段信息 根据条件查询店铺以及商品信息
     * @param product 对应Product实体类
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopProduct", method = RequestMethod.POST)
    public ResultEntity getShopProduct(@RequestBody Product product) {
        String productType = product.getProductType();
        List<ShopCustom> shop_product = shopCustomService.getShop_Product(productType);
        if (shop_product != null) {
            return ResultEntity.success(shop_product);
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 智能排序(离我最近)：根据店铺距离排序 按升序排序 查询所有店铺商品信息
     *                     判断查询数据是否为空 若为空 则返回失败信息
     *                     若不为空 则返回成功信息以及携带数据
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopProductByDistance", method = RequestMethod.POST)
    public ResultEntity getShopProductByDistance() {
        List<ShopCustom> shop_productByDistance = shopCustomService.getShop_ProductByDistance();
        if (shop_productByDistance != null) {
            return ResultEntity.success(shop_productByDistance);
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 智能排序(好评优先)：根据店铺评分排序 按降序排序 查询所有店铺商品信息
     *                     判断查询数据是否为空 若为空 则返回失败信息
     *                     若不为空 则返回成功信息以及携带数据
     * @return
     */
    @RequestMapping(value = "/getShopProductByPoint", method = RequestMethod.POST)
    public ResultEntity getShopProductByPoint() {
        List<ShopCustom> shop_productByPoint = shopCustomService.getShop_ProductByPoint();
        if (shop_productByPoint != null) {
            return ResultEntity.success(shop_productByPoint);
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 智能排序(人气最高)：根据店铺销量排序 按降序排序 查询所有店铺商品信息
     *                     判断查询数据是否为空 若为空 则返回失败信息
     *                     若不为空 则返回成功信息以及携带数据
     * @return
     */
    @RequestMapping(value = "/getShopProductBySalesnum", method = RequestMethod.POST)
    public ResultEntity getShopProductBySalesnum() {
        List<ShopCustom> shop_productBySalesnum = shopCustomService.getShop_ProductBySalesnum();
        if (shop_productBySalesnum != null) {
            return ResultEntity.success(shop_productBySalesnum);
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 根据是否免约 是否节假日可用 是否是新店 三个条件查询店铺商品信息
     * @param shop shop类型实体
     * @return 返回一个ResultEntity类型的成功或者失败信息
     */
    @RequestMapping(value = "/getShopProductByExample", method = RequestMethod.POST)
    public ResultEntity getShopProductByExample(@RequestBody Shop shop) {
        if (shop != null) {
            String isfree = shop.getIsfree();
            String isholiday = shop.getIsholiday();
            Integer isnew = shop.getIsnew();
            List<ShopCustom> shop_productByExample = shopCustomService.getShop_ProductByExample(isfree, isholiday, isnew);
            if (shop_productByExample != null) {
                return ResultEntity.success(shop_productByExample);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 首页(店铺详细信息接口)：查询当前店铺的详细信息
     * @param shop shop类型实体
     * @return 返回一个ResultEntity类型的成功或者失败信息
     */
    @RequestMapping(value = "/getShopSetMeal", method = RequestMethod.POST)
    public ResultEntity getShopSetMeal(@RequestBody ShopCustom shop) throws ParseException {
        if (shop != null) {
            Integer shopId = shop.getShopId();
            String token = shop.getToken();
            if (token != null){
                Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
                Integer userId = (Integer) map.get("userId");
                boolean b = userBrowseService.addUserBrowse(userId, shopId);
            }
            List<ShopCustom> shopSetMeal = shopCustomService.getShopSetMeal(shopId);
            if (shopSetMeal != null) {
                return ResultEntity.success(shopSetMeal);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }
}
